function varargout = SettingsDlg(varargin)
% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal

% Edit the above text to modify the response to help SettingsDlg

% Last Modified by GUIDE v2.5 29-Jun-2011 22:43:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SettingsDlg_OpeningFcn, ...
                   'gui_OutputFcn',  @SettingsDlg_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before SettingsDlg is made visible.
function SettingsDlg_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SettingsDlg (see VARARGIN)

% Choose default command line output for SettingsDlg
handles.output = hObject;

parent=get(handles.figure1,'UserData');

handles.DisplayWidget=parent.DisplayWidget;
set(handles.TL_SWAN,'String',parent.SWANPath);
clear parent;

%update settings

%Axis settings
if strcmp(handles.DisplayWidget.getAxProp('XGrid'),'on')
set(handles.CK_AxGrid,'Value',true);
else
   set(handles.CK_AxGrid,'Value',false);
end
setColor(handles.PB_gridCol,handles.DisplayWidget.getAxProp('XColor'));
setColor(handles.PB_AxBg,handles.DisplayWidget.getAxProp('Color'));
axlim=handles.DisplayWidget.getAxesLimits();
set(handles.TL_Xmin,'String',axlim(1));
set(handles.TL_Xmax,'String',axlim(2));
set(handles.TL_Ymin,'String',axlim(3));
set(handles.TL_Ymax,'String',axlim(4));

%Computational grids settings
setColor(handles.PB_cMain,handles.DisplayWidget.getCGridColor('cMain',...
    'MarkerEdgeColor'));
setColor(handles.PB_cNest,handles.DisplayWidget.getCGridColor('cNest',...
    'MarkerEdgeColor'));
setColor(handles.PB_cCorner,handles.DisplayWidget.getCGridColor('cNest',...
    'MarkerFaceColor'));

%Control Points settings
setColor(handles.PB_External,...
    handles.DisplayWidget.getCPointProp('MarkerEdgeColor'));
setColor(handles.PB_Internal,...
    handles.DisplayWidget.getCPointProp('MarkerFaceColor'));
set(handles.TL_PointSize,'String',...
    handles.DisplayWidget.getCPointProp('MarkerSize'));

%Input Grids Settings
axis(handles.gradient1,'off');
axis(handles.gradient2,'off');

gridList={'BATHYMETRY','VEGETATION','CURRENT','WIND'};

for j=1:length(gridList)
    name=gridList{j};
    switch name
        case 'BATHYMETRY'
            [c1,c2]=handles.DisplayWidget.getGridColormap(name,'seamap');
            handles.iGrids.(name).seamap.c1=c1;
            handles.iGrids.(name).seamap.c2=c2;%last color
            [c1,c2]=handles.DisplayWidget.getGridColormap(name,'landmap');
            handles.iGrids.(name).landmap.c1=c1;%first color 
            handles.iGrids.(name).landmap.c2=c2;%last color
        otherwise
            [c1,c2]=handles.DisplayWidget.getGridColormap(name,'map');
            handles.iGrids.(name).map.c1=c1;
            handles.iGrids.(name).map.c2=c2;       
    end
end
set(handles.panelGrad1,'Visible','off');
set(handles.panelGrad2,'Visible','off');
set(handles.menu_grids,'String',gridList);


% Update handles structure
guidata(hObject, handles);

menu_grids_Callback(handles.menu_grids, [], handles);

% UIWAIT makes SettingsDlg wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = SettingsDlg_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in PB_gridCol.
function PB_gridCol_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);



% --- Executes on button press in CK_AxGrid.
function CK_AxGrid_Callback(hObject, eventdata, handles)



% --- Executes on selection change in menu_grids.
function menu_grids_Callback(hObject, eventdata, handles)
% hObject    handle to menu_grids (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

grids=get(hObject,'String');
grid=grids{get(hObject,'Value')};
switch grid
    case 'BATHYMETRY'
        set(handles.panelGrad1,'Visible','on');
        set(handles.panelGrad2,'Visible','on');
        setColor(handles.PB_grad11,handles.iGrids.(grid).seamap.c1);
        setColor(handles.PB_grad12,handles.iGrids.(grid).seamap.c2);
        [m,img]=handles.DisplayWidget.getGradient(handles.iGrids.(grid).seamap.c1,...
            handles.iGrids.(grid).seamap.c2);
        image('parent',handles.gradient1,'CData',img);
        
        setColor(handles.PB_grad21,handles.iGrids.(grid).landmap.c1);
        setColor(handles.PB_grad22,handles.iGrids.(grid).landmap.c2);
        [m,img]=handles.DisplayWidget.getGradient(handles.iGrids.(grid).landmap.c1,...
            handles.iGrids.(grid).landmap.c2);
        image('parent',handles.gradient2,'CData',img);
        
    otherwise
        set(handles.panelGrad1,'Visible','on');
        set(handles.panelGrad2,'Visible','off');
        setColor(handles.PB_grad11,handles.iGrids.(grid).map.c1);
        setColor(handles.PB_grad12,handles.iGrids.(grid).map.c2);
        [m,img]=handles.DisplayWidget.getGradient(handles.iGrids.(grid).map.c1,...
            handles.iGrids.(grid).map.c2);
        image('parent',handles.gradient1,'CData',img);
end

% Hints: contents = get(hObject,'String') returns menu_grids contents as cell array
%        contents{get(hObject,'Value')} returns selected item from menu_grids


% --- Executes during object creation, after setting all properties.
function menu_grids_CreateFcn(hObject, eventdata, handles)
% hObject    handle to menu_grids (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in PB_grad11.
function PB_grad11_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);
%update displayed gradient image
c2=getColor(handles.PB_grad12);
[m,img]=handles.DisplayWidget.getGradient(color,c2);
image('parent',handles.gradient1,'CData',img);
%store data in handes
saveIGridSettings(handles);

% --- Executes on button press in PB_grad12.
function PB_grad12_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);
saveIGridSettings(handles);

%update displayed gradient image
c1=getColor(handles.PB_grad11);
[m,img]=handles.DisplayWidget.getGradient(c1,color);
image('parent',handles.gradient1,'CData',img);


% --- Executes on button press in PB_AxBg.
function PB_AxBg_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);


% --- Executes on button press in PB_Save.
function PB_Save_Callback(hObject, eventdata, handles)
%store settings in DisplayWidget and update Display to show all changes.

SWANPath=get(handles.TL_SWAN,'String');
%it's not elegant, but works.
handles.DisplayWidget.trickSWANPath(SWANPath);
%axes properties
if get(handles.CK_AxGrid,'Value')
    handles.DisplayWidget.setAxProp('XGrid','on');
    handles.DisplayWidget.setAxProp('YGrid','on');
else
    handles.DisplayWidget.setAxProp('XGrid','off');
    handles.DisplayWidget.setAxProp('YGrid','off');
end
handles.DisplayWidget.setAxProp('XColor',getColor(handles.PB_gridCol));
handles.DisplayWidget.setAxProp('YColor',getColor(handles.PB_gridCol));
handles.DisplayWidget.setAxProp('Color',getColor(handles.PB_AxBg));
xmin=str2double(get(handles.TL_Xmin,'String'));
xmax=str2double(get(handles.TL_Xmax,'String'));
ymin=str2double(get(handles.TL_Ymin,'String'));
ymax=str2double(get(handles.TL_Ymax,'String'));
handles.DisplayWidget.setAxesLimits(xmin,xmax,ymin,ymax);
%Computational grids settings
handles.DisplayWidget.setCGridColor('cMain','MarkerEdgeColor',...
    getColor(handles.PB_cMain));
handles.DisplayWidget.setCGridColor('cMain','MarkerFaceColor',...
    getColor(handles.PB_cCorner));
handles.DisplayWidget.setCGridColor('cNest','MarkerEdgeColor',...
    getColor(handles.PB_cNest));
handles.DisplayWidget.setCGridColor('cNest','MarkerFaceColor',...
    getColor(handles.PB_cCorner));

%Control Points settings
handles.DisplayWidget.setCPointProp('MarkerEdgeColor',...
    getColor(handles.PB_External));
handles.DisplayWidget.setCPointProp('MarkerFaceColor',...
    getColor(handles.PB_Internal));
handles.DisplayWidget.setCPointProp('MarkerSize',...
    str2double(get(handles.TL_PointSize,'String')));

%Input Grids Settings

gridList=fieldnames(handles.iGrids);

for j=1:length(gridList)
    name=gridList{j};
    switch name
        case 'BATHYMETRY'
            c1=handles.iGrids.(name).seamap.c1;
            c2=handles.iGrids.(name).seamap.c2;
            c3=handles.iGrids.(name).landmap.c1;
            c4=handles.iGrids.(name).landmap.c2;
            handles.DisplayWidget.setGridColormap(name,'seamap',c1,c2);
            handles.DisplayWidget.setGridColormap(name,'landmap',c3,c4);            
        otherwise
            
            c1=handles.iGrids.(name).map.c1;
            c2=handles.iGrids.(name).map.c2;
            handles.DisplayWidget.setGridColormap(name,'map',c1,c2);
    end
end

%saves settings in XML file
saveSettingsXML(handles);
%update display
handles.DisplayWidget.refreshDisplay();
%close figure
close(handles.figure1);


function TL_SWAN_Callback(hObject, eventdata, handles)
% hObject    handle to TL_SWAN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TL_SWAN as text
%        str2double(get(hObject,'String')) returns contents of TL_SWAN as a double


% --- Executes during object creation, after setting all properties.
function TL_SWAN_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TL_SWAN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in PB_SWAN.
function PB_SWAN_Callback(hObject, eventdata, handles)
[fname,fpath]=uigetfile('*');
set(handles.TL_SWAN,'String',fullfile(fpath,fname));

% --- Executes on button press in PB_External.
function PB_External_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);

% --- Executes on button press in PB_Internal.
function PB_Internal_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);

% --- Executes on button press in PB_cMain.
function PB_cMain_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);

% --- Executes on button press in PB_cNest.
function PB_cNest_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);

% --- Executes on button press in PB_cCorner.
function PB_cCorner_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);

% --- Executes on button press in PB_grad21.
function PB_grad21_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);
%update displayed gradient image
c2=getColor(handles.PB_grad22);
[m,img]=handles.DisplayWidget.getGradient(color,c2);
image('parent',handles.gradient2,'CData',img);
saveIGridSettings(handles);

% --- Executes on button press in PB_grad22.
function PB_grad22_Callback(hObject, eventdata, handles)
color=uisetcolor();
setColor(hObject,color);
%update displayed gradient image
c1=getColor(handles.PB_grad21);
[m,img]=handles.DisplayWidget.getGradient(c1,color);
image('parent',handles.gradient2,'CData',img);
saveIGridSettings(handles);

function TL_Xmin_Callback(hObject, eventdata, handles)
% hObject    handle to TL_Xmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TL_Xmin as text
%        str2double(get(hObject,'String')) returns contents of TL_Xmin as a double


% --- Executes during object creation, after setting all properties.
function TL_Xmin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TL_Xmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function TL_Xmax_Callback(hObject, eventdata, handles)
% hObject    handle to TL_Xmax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TL_Xmax as text
%        str2double(get(hObject,'String')) returns contents of TL_Xmax as a double


% --- Executes during object creation, after setting all properties.
function TL_Xmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TL_Xmax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function TL_Ymin_Callback(hObject, eventdata, handles)
% hObject    handle to TL_Ymin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TL_Ymin as text
%        str2double(get(hObject,'String')) returns contents of TL_Ymin as a double


% --- Executes during object creation, after setting all properties.
function TL_Ymin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TL_Ymin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function TL_Ymax_Callback(hObject, eventdata, handles)
% hObject    handle to TL_Ymax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TL_Ymax as text
%        str2double(get(hObject,'String')) returns contents of TL_Ymax as a double


% --- Executes during object creation, after setting all properties.
function TL_Ymax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TL_Ymax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function TL_PointSize_Callback(hObject, eventdata, handles)
% hObject    handle to TL_PointSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TL_PointSize as text
%        str2double(get(hObject,'String')) returns contents of TL_PointSize as a double


% --- Executes during object creation, after setting all properties.
function TL_PointSize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TL_PointSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in PB_Cancel.
function PB_Cancel_Callback(hObject, eventdata, handles)
% hObject    handle to PB_Cancel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(handles.figure1);
%%%%%%%------------------------%%%%%%%

function setColor(obj,color)

c=zeros(10,10,3);
c(:,:,1)=color(1);
c(:,:,2)=color(2);
c(:,:,3)=color(3);
set(obj,'CData',c);


function color=getColor(obj)

data=get(obj,'CData');
color=data(1,1,:);

function val=getStatus(obj)

data=get(obj,'Value');
if data
    val=true;
else
    val=false;
end

function saveIGridSettings(handles)

grids=get(handles.menu_grids,'String');
grid=grids{get(handles.menu_grids,'Value')};

switch grid
    case 'BATHYMETRY'
        c1=getColor(handles.PB_grad11);
        c2=getColor(handles.PB_grad12);
        c3=getColor(handles.PB_grad21);
        c4=getColor(handles.PB_grad22);
        
        handles.iGrids.(grid).seamap.c1=c1;
        handles.iGrids.(grid).seamap.c2=c2;
        handles.iGrids.(grid).landmap.c1=c3;
        handles.iGrids.(grid).landmap.c2=c4;
                
    otherwise
        c1=getColor(handles.PB_grad11);
        c2=getColor(handles.PB_grad12);
        handles.iGrids.(grid).map.c1=c1;
        handles.iGrids.(grid).map.c2=c2;
end

guidata(handles.figure1,handles);


function saveSettingsXML(handles)

%get CISNE path, where settings.xml will be saved

path=mfilename('fullpath');
path=fileparts(path);

xmlDoc = com.mathworks.xml.XMLUtils.createDocument('CISNE_SETTINGS');
docNode = xmlDoc.getDocumentElement;
docNode.setAttribute('XML_version','1.0'); %use variable

%SWAN Path
data_node = xmlDoc.createElement('SWAN_Path');
docNode.appendChild(data_node);
textNode = xmlDoc.createTextNode(get(handles.TL_SWAN,'String'));
data_node.appendChild(textNode);

%Axes properties
Axes_node = xmlDoc.createElement('Axes');
docNode.appendChild(Axes_node);

data_node = xmlDoc.createElement('XGrid');
Axes_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getStatus(handles.CK_AxGrid)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('YGrid');
Axes_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getStatus(handles.CK_AxGrid)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('XColor');
Axes_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_gridCol)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('YColor');
Axes_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_gridCol)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('Color');
Axes_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_AxBg)));
data_node.appendChild(textNode);

%Computational Grids properties
CGrid_node = xmlDoc.createElement('CGrids');
docNode.appendChild(CGrid_node);

data_node = xmlDoc.createElement('MarkerEdgeColor');
data_node.setAttribute('kind','cMain');
CGrid_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_cMain)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('MarkerFaceColor');
data_node.setAttribute('kind','cMain');
CGrid_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_cCorner)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('MarkerEdgeColor');
data_node.setAttribute('kind','cNest');
CGrid_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_cNest)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('MarkerFaceColor');
data_node.setAttribute('kind','cNest');
CGrid_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_cCorner)));
data_node.appendChild(textNode);

%Control Points properties
CPoint_node = xmlDoc.createElement('CPoints');
docNode.appendChild(CPoint_node);

data_node = xmlDoc.createElement('MarkerEdgeColor');
CPoint_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_External)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('MarkerFaceColor');
CPoint_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(getColor(handles.PB_Internal)));
data_node.appendChild(textNode);

data_node = xmlDoc.createElement('MarkerSize');
CPoint_node.appendChild(data_node);
textNode = xmlDoc.createTextNode(data2txt(get(handles.TL_PointSize,'String')));
data_node.appendChild(textNode);

%Input Grids properties
IGrid_node = xmlDoc.createElement('IGrids');
docNode.appendChild(IGrid_node);

gridList=fieldnames(handles.iGrids);

for j=1:length(gridList)
    name=gridList{j};
	grid_node=xmlDoc.createElement(name);
    IGrid_node.appendChild(grid_node);
    switch name
        case 'BATHYMETRY'
            c1=handles.iGrids.(name).seamap.c1;
            c2=handles.iGrids.(name).seamap.c2;
            c3=handles.iGrids.(name).landmap.c1;
            c4=handles.iGrids.(name).landmap.c2;
			
			data_node = xmlDoc.createElement('landmap');
			grid_node.appendChild(data_node);
			textNode = xmlDoc.createTextNode(sprintf('%g %g %g;',c3,c4));
			data_node.appendChild(textNode);
			
			data_node = xmlDoc.createElement('seamap');
			grid_node.appendChild(data_node);
			textNode = xmlDoc.createTextNode(sprintf('%g %g %g;',c1,c2));
			data_node.appendChild(textNode);
      
        otherwise
            c1=handles.iGrids.(name).map.c1;
            c2=handles.iGrids.(name).map.c2;
			
			data_node = xmlDoc.createElement('map');
			grid_node.appendChild(data_node);
			textNode = xmlDoc.createTextNode(sprintf('%g %g %g;',c1,c2));
			data_node.appendChild(textNode);

    end
end

xmlwrite(fullfile(path,'settings.xml'),xmlDoc);


function text=data2txt(data)

if islogical(data)
    if data
        text='on';
    else
        text='off';
    end
elseif isnumeric(data)
    text=sprintf('%g ',data);
else
    text=char(data);
end